<div class="card">
	<div class="card-content">
		<span class="card-title">
			PESERTA UJIAN <?= $ujian->XNamaMapel; ?>
		</span>
		<div class="row">
			<div class="col s6">
				<table>
					<tr>
						<th>Kode Ujian</th>
						<td>:</td>
						<td><?= $ujian->XKodeUjian; ?></td>
					</tr>
					<tr>
						<th>Token Ujian</th>
						<td>:</td>
						<td><?= $ujian->XTokenUjian; ?></td>
					</tr>
					<tr>
						<th>Waktu Mengerjakan</th>
						<td>:</td>
						<td><?= $ujian->XLamaUjian; ?> Menit</td>
					</tr>
				</table>
			</div>
			<div class="col s6">
				<table>
					<tr>
						<th>Siswa Belum Mengerjakan</th>
						<td>:</td>
						<td id="belumMengerjakan"></td>
					</tr>
					<tr>
						<th>Siswa Sedang Mengerjakan</th>
						<td>:</td>
						<td id="sedangMengerjakan"></td>
					</tr>	
					<tr>
						<th>Siswa Selesai Mengerjakan</th>
						<td>:</td>
						<td id="selesaiMengerjakan"></td>
					</tr>
					<tr>
						<th>Total Siswa</th>
						<td>:</td>
						<td><span id="totalSiswa"></span></td>
					</tr>
				</table>
			</div>
		</div>
	</div>
	<div class="card-action">
		<a href="<?= base_url("admin/peserta_ujian"); ?>" class="btn blue">Ganti Ujian</a>
		<!-- <a href="<?= base_url("admin/peserta_ujian/ruang/".$Urut); ?>" class="btn green">Ganti Ruang</a> -->
	</div>
</div>

<div class="card-panel blue white-text">
	<strong>
		Halaman	di reload otomatis dalam 10 menit
	</strong>
	<button onclick="window.location.reload()" class="pull-right btn white black-text" type="button"><i class="material-icons left">refresh</i> Reload sekarang</button>
</div>

<div class="card-panel">
	<table id="siswa" class="table striped bordered">
		<thead>
			<tr>
				<th style="width: 1%">No.</th>
				<th>Nomer Ujian</th>
				<th>Nama</th>
				<th>Kelas</th>
				<th>Status</th>
				<th>Waktu Start</th>
				<th>Digunakan</th>
				<th>Sisa Waktu</th>
				<th>#</th>
			</tr>
		</thead>
		<tbody>
			<?php
			$no=1;
			$hitung=[
				'belum' => 0,
				'mengikuti' => 0,
				'selesai' => 0,
				'total'	=> 0
			];
			foreach ($siswa->result() as $s) {
				$hitung['total']++;
				?>
				<tr>
					<td><?= $no++; ?>.</td>
					<td><?= $s->XNomerUjian; ?></td>
					<td><?= $s->XNamaSiswa; ?></td>
					<td><?= $s->XNamaKelas; ?></td>

					<?php
					// $this->db->select("XStatusUjian");
					$this->db->where("XIdUjian",$ujian->XIdUjian);
					$this->db->where("XNomerUjian",$s->XNomerUjian);
					$ss=$this->db->get("cbt_siswa_ujian");
					if ($ss->num_rows() < 1) {
						$hitung['belum']++;
						echo "<td><span class='red-text'>Belum Memulai Ujian</span></td><td></td><td></td><td></td><td></td>";
					} else {
						$ss=$ss->row();
						$sekarang=mktime(date('H'),date('i'),date("s")+$ss->XSisaWaktu,date('m'),date('d'),date('Y'));
						// echo($ss->num_rows());
						$sisa = $waktu_selesai - $sekarang;
						$jumlah_jam = floor($sisa/3600);
						$sisa = $sisa % 3600;
						$jumlah_menit = floor($sisa/60);
						$sisa = $sisa % 60;
						$jumlah_detik = floor($sisa/1);

						$data['waktu_selesai']=[
							"jam"=>$jumlah_jam,
							"menit"=>$jumlah_menit,
							"detik"=>$jumlah_detik,
						];

						$sisa = $ss->XSisaWaktu ;
						$jumlah_jam = floor($sisa/3600);
						$sisa = $sisa % 3600;
						$jumlah_menit = floor($sisa/60);
						$sisa = $sisa % 60;
						$jumlah_detik = floor($sisa/1);

						$data['waktu_digunakan']=[
							"jam"=>$jumlah_jam,
							"menit"=>$jumlah_menit,
							"detik"=>$jumlah_detik,
						];

						?>

						<td>
							<?php
							if ($ss->XStatusUjian == 0) {
								$hitung['mengikuti']++;
								echo "<span class='blue-text'>Sedang Ujian</span>";
							} else {
								$hitung['selesai']++;
								echo "<span class='green-text'>Selesai Ujian</span>";
							}
							?>
						</td>
						<td>
							<?= date("Y-M-d  H:i", strtotime($ss->XMulaiUjian)) ?>
						</td>
						<td>
							<?= sprintf("%02s", $data['waktu_digunakan']['jam']); ?>:<?= sprintf("%02s", $data['waktu_digunakan']['menit']); ?>:<?= sprintf("%02s", $data['waktu_digunakan']['detik']); ?>
						</td>
						<td>
							<?= sprintf("%02s", $data['waktu_selesai']['jam']); ?>:<?= sprintf("%02s", $data['waktu_selesai']['menit']); ?>:<?= sprintf("%02s", $data['waktu_selesai']['detik']); ?>
						</td>
						<td>
							<a target="_blank" class="btn blue darken-2" href="<?= base_url('admin/jawaban/lihat/'. $ss->Urut); ?>"><i class="material-icons left"> assignment</i> Lihat jawaban</a>
							<?php
							if ($ss->XStatusUjian == 0) {
								?>
								<a class="btn purple" href="<?= base_url('admin/peserta_ujian/reset_waktu/'. $ss->Urut); ?>"><i class="material-icons left">alarm</i> Reset Waktu</a>
								<a class="btn green" href="<?= base_url('admin/peserta_ujian/selesai/'. $ss->Urut); ?>"><i class="material-icons left">assignment_turned_in</i> selesai ujian</a>
								<?php
							} else {
								?>
								<a class="btn danger" href="<?= base_url('admin/peserta_ujian/hapus_nilai/'. $ss->Urut); ?>"><i class="material-icons left">delete</i> hapus nilai</a>
								<?php
							}
							?>
						</td>
						<?php
					}
					?>
				</tr>
				<?php
			}
			?>
		</tbody>
	</table>
</div>
<script type="text/javascript">
	$(document).ready(function() {
		$("table#siswa").dataTable({
			responsive:true
		});

		$("#belumMengerjakan").html('<?= $hitung['belum']; ?>');
		$("#selesaiMengerjakan").html('<?= $hitung['selesai']; ?>');
		$("#sedangMengerjakan").html('<?= $hitung['mengikuti']; ?>');
		$("#totalSiswa").html('<?= $hitung['total']; ?>');

		setInterval(() => {
			window.location.reload();
		},600000);
	});
</script>